﻿using PLCS.App.UserAuthorization;
using PLCS.Application.Contracts.LogShowApp.Dtos;
using PLCS.Application.Contracts.RoleApp.Dtos;
using PLCS.Client.IService.LogManager;

namespace ZFGG_JTX_PLCS.App.UserAuthorization.ViewModels.Role;

public class RoleDetailViewModel : HostDialogViewModel
{
    #region 字段

    private ObservableCollection<string> _canSelectItems;
    private string _selectItem;
    private ObservableCollection<string> _selectedItems;
    private readonly IMenuService _menuService;
    private readonly IRoleService _roleService;
    private readonly IObjectMapper<UserAuthorizationModule> _objectMapper;
    private readonly IOperationLogService _logShowService;
    private RoleDto _model;

    public ObservableCollection<string> CanSelectItems
    {
        get { return _canSelectItems; }
        set { SetProperty(ref _canSelectItems, value); }
    }

    public string SelectItem
    {
        get { return _selectItem; }
        set { SetProperty(ref _selectItem, value); }
    }

    public ObservableCollection<string> SelectedItems
    {
        get { return _selectedItems; }
        set { SetProperty(ref _selectedItems, value); }
    }

    public RoleDto Model
    {
        get { return _model; }
        set { SetProperty(ref _model, value); }
    }

    #endregion 字段

    #region DelegateCommand

    public DelegateCommand DropOpenedCommand => new DelegateCommand(DropOpened);

    private async void DropOpened()
    {
        try
        {
            CanSelectItems = new ObservableCollection<string>();
            var menus = await _menuService.GetListAsync();
            foreach (var item in menus)
            {
                if (!SelectedItems.Contains(item.Title) && string.IsNullOrEmpty(item.ViewName))
                    CanSelectItems.Add(item.Title);
            }
        }
        catch (Exception ex)
        {
        }
    }

    public DelegateCommand AddItemCommand => new DelegateCommand(() =>
    {
        SelectedItems.Add(SelectItem);
    });

    public DelegateCommand<object> DeleteItemCommand => new DelegateCommand<object>(index =>
    {
        SelectedItems.RemoveAt(int.Parse(index.ToString()));
    });

    #endregion DelegateCommand

    public override void OnDialogOpened(IDialogParameters parameters)
    {
        SelectedItems = new ObservableCollection<string>();
        if (parameters.ContainsKey("Value"))
        {
            Model = parameters.GetValue<RoleDto>("Value");
            if (!string.IsNullOrEmpty(Model.MenusString))
                foreach (var item in Model.MenusString.Split(ConstantChar.SplitChar))
                {
                    SelectedItems.Add(item);
                };
            Title = "编辑";
        }
        else
        {
            Title = "新增";
            Model = new RoleDto();
        }
    }

    protected override async void Save()
    {
        try
        {
            SavebuttonIsEnabled = false;
            var role = _objectMapper.Map<RoleDto, RoleEditDto>(Model);
            string res = "";
            for (int i = 0; i < SelectedItems.Count; i++)
            {
                if (i == SelectedItems.Count - 1)
                {
                    res += $"{SelectedItems[i]}";
                    continue;
                }
                res += $"{SelectedItems[i]}{ConstantChar.SplitChar}";
            }
            role.MenusString = res;
            if (role.Id != Guid.Empty)
                await _roleService.UpdateAsync(role);
            else
                await _roleService.CreateAsync(role);
            ShowMessage("操作成功");
            base.Save();
        }
        catch (Exception ex)
        {
            ShowMessage("操作失败");
        }
        finally { SavebuttonIsEnabled = true; }
    }

    public RoleDetailViewModel(IMenuService menuService, IRoleService roleService, IObjectMapper<UserAuthorizationModule> objectMapper, IOperationLogService logShowService)
    {
        this._menuService = menuService;
        this._roleService = roleService;
        this._objectMapper = objectMapper;
        this._logShowService = logShowService;
    }
}